<template>
  <div>
    <!-- 年和月 -->
    <el-row type="flex" justify="end">
      <!-- 年 用组件给定一个日期，日期获取年，年取前五年 + 后五年 -->
      <el-select
        v-model="currentYear"
        size="small"
        style="width: 120px"
        @change="dataChange"
      >
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        /> </el-select>=={{ currentYear }}
      <el-select
        v-model="currentMonth"
        size="small"
        style="width: 120px;margin-left: 10px"
        @change="dataChange"
      >
        <!-- 循环 -->
        <el-option
          v-for="item in 12"
          :key="item"
          :label="item"
          :value="item"
        /> </el-select>=={{ currentMonth }}
    </el-row>
    <el-calendar v-model="value">
      <template slot="dateCell" slot-scope="{ date, data }">
        <!-- <pre>{{ data.day }}</pre> -->
        <!--   data.day.split('-')===[2021,06,29] -->
        <p :class="data.isSelected ? 'is-selected' : ''">
          {{
            data.day
              .split('-')
              .slice(2)
              .join()
          }}
          <span
            v-if="isWeek(date)"
            style="background:#fa7c4d;padding:2px;display:inline-block;text-align:center;width:25px;height:25px;line-height:25px;border-radius:50%;color:#fff;"
          >休</span>
        </p>
      </template>
    </el-calendar>
  </div>
</template>
<script>
export default {
  props: {
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      value: new Date(),

      currentYear: null,

      currentMonth: null,
      yearList: []
    }
  },
  created() {
    // 1- 获取年月
    this.currentYear = this.startDate.getFullYear()
    this.currentMonth = this.startDate.getMonth() + 1
    this.yearList = Array.from(
      Array(11),
      (val, index) => this.currentYear - 5 + index
    )
  },
  methods: {
    dataChange() {
      this.value = new Date(`${this.currentYear}-${this.currentMonth}-1`)
    },
    isWeek(date) {
      if (date.getDay() === 6 || date.getDay() === 0) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-calendar__header {
  display: none;
}
::v-deep .el-calendar-day {
  height: auto;
}
::v-deep .el-calendar-table__row td,
::v-deep .el-calendar-table tr td:first-child,
::v-deep .el-calendar-table__row td.prev {
  border: none;
}
.is-selected {
  background-color: rgb(64, 158, 255);
  color: #fff;
}
</style>
